/*
 * Copyright (c) 2020 hebeiyiliuba.com
 * All rights reserved.
 *
 */
package org.jeecg.modules.middleware.quartz.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.third.nucarf.NucarfEtcService;
import org.jeecg.modules.third.nucarf.dto.NucarEtcResponderDTO;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hynucaretcinvoice.entity.HyNucaretcInvoice;
import org.jeecg.modules.wlhy.hynucaretcinvoice.service.IHyNucaretcInvoiceService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.PersistJobDataAfterExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 查询etc发票
 *
 * @author duanlikao
 * @created 2021/10/18
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class QueryEtcInvoiceService implements Job {
    private static final Logger LOGGER = LoggerFactory.getLogger(QueryEtcInvoiceService.class);

    @Autowired
    private IHyShippingNoteService shippingNoteService;

    @Autowired
    private IHyNucaretcInvoiceService hyNucaretcInvoiceService;

    @Autowired
    private NucarfEtcService nucarfEtcService;

    //@Scheduled(cron = "0 01 4 * * ?")//每天的04:01:00执行
//    @Scheduled(cron = "0 */05 * * * ?")//每天的04:01:00执行
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        HyConfig systemConfig = HyConfigUtils.getConfig();
        if(systemConfig == null || !systemConfig.getEtcwdIzUse()){
            LOGGER.info("万金油外调车ETC服务暂未开启,请联系管理。");
            return;
        }


        QueryWrapper<HyShippingNote> queryWrapper = new QueryWrapper();
        queryWrapper.eq("upload_nucar_etc","1");
        queryWrapper.eq("waybill_status","2");
        queryWrapper.eq("etc_type","1");

        List <HyShippingNote> shippingNotes =  shippingNoteService.list(queryWrapper);

        if(CollectionUtils.isEmpty(shippingNotes)) {
            LOGGER.info("QueryEtcInvoiceService queryEtcInvoice no data handling ");
            return;
        }
        Long start = System.currentTimeMillis();
        LOGGER.info("QueryEtcInvoiceService queryEtcInvoice:执行查询etc发票数据开始" + DateUtils.getDateStr());
        for(HyShippingNote hyShippingNote:shippingNotes){
            try {
                NucarEtcResponderDTO nucarEtcResponderDTO = nucarfEtcService.findInvoiceByNum(hyShippingNote.getShippingNoteNumber());
                if(nucarEtcResponderDTO == null ){
                    continue;
                }
                Integer waybillStatus = nucarEtcResponderDTO.getWaybillStatus();
                if(waybillStatus != null  &&  3 == waybillStatus){
                    if( CollectionUtils.isNotEmpty(nucarEtcResponderDTO.getResult())) {
                        List<HyNucaretcInvoice> result = nucarEtcResponderDTO.getResult();
                        nucarfEtcService.saveEtcInvoice(result, hyShippingNote);
                    }
                }
            }catch (Exception e){
                LOGGER.info("QueryEtcInvoiceService queryEtcInvoice:Exception : {}" ,e.getMessage());
                continue;
            }
        }
        LOGGER.info("QueryEtcInvoiceService queryEtcInvoice:执行查询etc发票数据结束,耗时:"+(System.currentTimeMillis()-start)+"毫秒----");

    }



}
